﻿using System;
using System.Collections.Generic;
using System.Text;
using Gammar.Digger.DALFactory;
using Gammar.Digger.Entity;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.IDAL;
using System.Data;
using Gammar.Digger.BLL.Common;
using System.Web;
namespace Gammar.Digger.BLL
{
    public class WebSiteManagement
    {
        static Iweb_site web = DataAccess.Createweb_site();
        public static SortList<web_siteEntity> GetAllWebSite()
        {
           
            if (CacheHelper.Get(CacheKey.Db_GPriceCollect_WebSite)!=null)
            {
                return (SortList < web_siteEntity >)CacheHelper.Get(CacheKey.Db_GPriceCollect_WebSite);
            }
            else
            {
                Iweb_site web = DataAccess.Createweb_site();
                SortList<web_siteEntity> weblist = web.GetList("");
                CacheHelper.Insert(CacheKey.Db_GPriceCollect_WebSite, weblist, 60 * 60);
                return weblist;
            }
       
            

        }

        public static DataTable GetAllWebSiteWithCategoryName()
        {
            SortList<web_siteEntity> weblist = GetAllWebSite();
            Dictionary<string,string> dictCategory = GetAllWebSiteCategory();
            DataTable dt = new DataTable();
            dt.Columns.Add("category_name");
            foreach (web_siteEntity site in weblist)
            {
                Dictionary<string, object> dict = DataUtility.GetModelProperties(site);

                DataRow row = dt.NewRow();
                foreach (string key in dict.Keys)
                {
                    if (!dt.Columns.Contains(key))
                    {
                        dt.Columns.Add(key);
                    }
                    row[key]=dict[key];
                }
                row["category_name"] = dictCategory[site.category_id.ToString()];
                
                dt.Rows.Add(row);
            }
            return dt;
        }

        public static web_siteEntity GetSiteById(string siteid)
        {
            string where = string.Empty;
            if (!string.IsNullOrEmpty(siteid))
            {
                where = "site_id="+siteid;
            }
            web = DataAccess.Createweb_site();
            return web.GetEntity(int.Parse(siteid));
        }

        public static string CreateNewWebSite(string name, string desc, string categoryid)
        {
            web_siteEntity site = new web_siteEntity();
            site.name = name;
            site.description = desc;
            Guid g = new Guid(categoryid);
            site.category_id = g;

            try
            {
               int reval= web.Add(site);
               if (reval == 1) { CacheHelper.Remove(CacheKey.Db_GPriceCollect_WebSite); }
               return "创建网站成功";
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return "失败："+ex.Message;
            }
        }

        public static string UpdateWebSite(string name, string desc, string categoryid, string siteid)
        {
            web_siteEntity site = new web_siteEntity();
            site.site_id = int.Parse(siteid);
            site.name = name;
            site.description = desc;
            site.category_id = new Guid(categoryid);

            try
            {
                int reval= web.Update(site);
                if (reval == 1) { CacheHelper.Remove(CacheKey.Db_GPriceCollect_WebSite); }
                return "更新网站成功";
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return "失败：" + ex.Message;
            }
        }


        public static DataTable GetWebSiteParentCategory()
        {
            DataTable dtreval = null;

            object obj=CacheHelper.Get(CacheKey.Db_GPriceWeb_ParentClass);
            if (obj!=null)
            {
                dtreval = (DataTable)obj;
            }
            else
            {
                IParent_Class pclass = DataAccess.CreateParent_Class();
                dtreval= pclass.GetTable(string.Empty);
                CacheHelper.Insert(CacheKey.Db_GPriceWeb_ParentClass, dtreval, 60 * 60);
            }
            return dtreval;

           
        }

        public static DataTable GetWebSiteChildCategory()
        {
            DataTable dtreval = null;

            object obj = CacheHelper.Get(CacheKey.Db_GPriceWeb_ChildClass);
            if (obj != null)
            {
                dtreval = (DataTable)obj;
            }
            else
            {
                IChild_Class cclass = DataAccess.CreateChild_Class();
                dtreval = cclass.GetTable(string.Empty);
                CacheHelper.Insert(CacheKey.Db_GPriceWeb_ChildClass, dtreval,60*60);
            }
            return dtreval;
        }

        public static Dictionary<string,string> GetAllWebSiteCategory()
        {
            DataTable dtChild=GetWebSiteChildCategory();
            DataTable dtParent = GetWebSiteParentCategory();
            Dictionary<string, string> reval = new Dictionary<string, string>();
            reval.Add("", "---");
            foreach (DataRow row in dtParent.Rows)
            {
                reval.Add(row["PClass_ID"].ToString(),"｜－"+ row["PClass_Name"].ToString());

                DataRow[] rows = dtChild.Select("PClass_ID='" + row["PClass_ID"].ToString()+"'");
                if (rows.Length>0)
                {
                    foreach (DataRow childRow in rows)
                    {
                        reval.Add(childRow["CClass_ID"].ToString(), "　　｜－" + childRow["CClass_Name"].ToString());
                    }
                }
            }

            return reval;
        }
    }
}
